import { Color, DrawPoint, QUI_BaseElement, QUI_Canvas, QUI_ElementType, QUI_Resource, Sprite, Vector2, Vector3 } from "../common.js";

//这是一个画线控件，
export class QUI_Line extends QUI_BaseElement {
    GetElementType(): QUI_ElementType {
        return QUI_ElementType.Element_User;
    }
    constructor() {
        super();
        this.white = QUI_Resource.GetWhiteSprite();
    }
    pointA: Vector2;
    pointB: Vector2;
    private white: Sprite;
    color: Color = Color.White;
    private dx: number = 0;
    private dy: number = 0;
    OnUpdate(canvas: QUI_Canvas, delta: number): void {
        let dir = Vector2.Dir(this.pointA, this.pointB);
        this.dx = dir.Y;
        this.dy = -dir.X;
    }
    // 在canvas上绘制图形
    OnRender(canvas: QUI_Canvas): void {
        // 调用batcherUI的DrawQuads方法，绘制图形

        let rectbuf = Sprite._rectbuf
        while (rectbuf.length < 4) {
            rectbuf.push(new DrawPoint());
        }

        let _color = this.color;

        let brect = this.GetWorldRect();
        let x = this.pointA.X + brect.X;
        let y = this.pointA.Y + brect.Y;
        let x2 = this.pointB.X + brect.X;
        let y2 = this.pointB.Y + brect.Y;
        rectbuf[0].x = x - this.dx //+ sx * this.border.XLeft;
        rectbuf[0].y = y - this.dy //+ sy * this.border.YTop;
        rectbuf[0].u = this.white.uv.U1;
        rectbuf[0].v = this.white.uv.V1;
        rectbuf[0].r = _color.R;
        rectbuf[0].g = _color.G;
        rectbuf[0].b = _color.B;
        rectbuf[0].a = _color.A;
        rectbuf[0].uvlayer = this.white.uvlayer;
        //rectbuf[0].paly = 0;
        rectbuf[0].eff = this.white.effect

        rectbuf[1].x = x + this.dx//- sx * this.border.XRight;
        rectbuf[1].y = y + this.dy//+ sy * this.border.YTop;
        rectbuf[1].u = this.white.uv.U2;
        rectbuf[1].v = this.white.uv.V1;
        rectbuf[1].r = _color.R
        rectbuf[1].g = _color.G
        rectbuf[1].b = _color.B
        rectbuf[1].a = _color.A
        rectbuf[1].uvlayer = this.white.uvlayer;
        //rectbuf[1].paly = 0;
        rectbuf[1].eff = this.white.effect;

        rectbuf[2].x = x2 - this.dx// + sx * this.border.XLeft;
        rectbuf[2].y = y2 - this.dy// - sy * this.border.YBottom;
        rectbuf[2].u = this.white.uv.U1;
        rectbuf[2].v = this.white.uv.V2;
        rectbuf[2].r = _color.R
        rectbuf[2].g = _color.G
        rectbuf[2].b = _color.B
        rectbuf[2].a = _color.A
        rectbuf[2].uvlayer = this.white.uvlayer;
        //rectbuf[2].paly = 0;
        rectbuf[2].eff = this.white.effect;

        rectbuf[3].x = x2 + this.dx// - sx * this.border.XRight;
        rectbuf[3].y = y2 + this.dy// - sy * this.border.YBottom;
        rectbuf[3].u = this.white.uv.U2;
        rectbuf[3].v = this.white.uv.V2;
        rectbuf[3].r = _color.R
        rectbuf[3].g = _color.G
        rectbuf[3].b = _color.B
        rectbuf[3].a = _color.A
        rectbuf[3].uvlayer = this.white.uvlayer;
        //rectbuf[3].paly = 0;
        rectbuf[3].eff = this.white.effect;
        canvas.batcherUI.DrawQuads(this.white.material, rectbuf, 1)
    }

}